﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace IronCommander.Utils
{
    [ValueConversion(typeof(long), typeof(string))]
    public class FileSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            var size = (long)value;

            double number;
            string postfix;

            if (size < 1024)
            {
                number = size;
                postfix = "b";
            }
            else if (size < 1024 * 1024)
            {
                number = size / 1024.0;
                postfix = "Kb";
            }
            else if (size < 1024 * 1024 * 1024)
            {
                number = size / 1024.0 / 1024.0;
                postfix = "Mb";
            }
            else 
            {
                number = size / 1024.0 / 1024.0 / 1024.0;
                postfix = "Gb";
            }

            return string.Format("{0:0.##} {1}", number, postfix);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
